home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 …SCII & the Runetime Code / ADC Developer CD (1992-07) (''Butch ASCII And The Runtime Code'')_iso / Dev.CD 199207.iso / Developer Essentials / DTS Sample Code / System 7.0 Samples / Kibitz / Print.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-21  |  7.9 KB  |  307 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** MultiFinder-Aware Shell Application
  5. **
  6. ** Program:     Kibitz
  7. ** File:        print.c
  8. ** Written by:  Eric Soldan
  9. ** Based on:    Code from Pete "Luke" Alexander.
  10. **
  11. ** Copyright © 1989-1991 Apple Computer, Inc.
  12. ** All rights reserved.
  13. */
  14.  
  15.  
  16.  
  17. /*****************************************************************************/
  18.  
  19.  
  20.  
  21. #include "Kibitz.h"                /* Get the Kibitz includes/typedefs, etc.    */
  22. #include "KibitzCommon.h"        /* Get the stuff in common with rez.        */
  23. #include "Kibitz.protos"        /* Get the prototypes for Kibitz.            */
  24.  
  25. #ifndef __ERRORS__
  26. #include <Errors.h>
  27. #endif
  28.  
  29. #ifndef __RESOURCES__
  30. #include <Resources.h>
  31. #endif
  32.  
  33.  
  34.  
  35. /*****************************************************************************/
  36.  
  37.  
  38.  
  39. pascal void            PrintIdleProc(void);
  40. short                gPrintPage;
  41. static DialogPtr    PrintingStatusDialog;
  42.  
  43.  
  44.  
  45. /*****************************************************************************/
  46.  
  47.  
  48.  
  49. /* This print-loop function is designed to be called under various situations.
  50. ** The big issue that it handles is finder printing.  If multiple documents
  51. ** are to be printed from the finder, the user should only see one job dialog
  52. ** for all the files.  (If a job dialog is shown for each file, how does the
  53. ** user know for which file the dialog is for?)  So, for situations where
  54. ** there is more than one file to be printed, call this code the first time
  55. ** with the firstJob boolean true.  Normally, the jobDlg boolean will also
  56. ** be true, except that under 7.0, you may be printing in the background.
  57. ** If this is the case, you don't want a job dialog for even the first file,
  58. ** and you should pass in false for the jobDlg boolean in this case.  For
  59. ** files 2-N, you should pass false for both booleans.  For regular application
  60. ** printing, you should pass true for both booleans, since the file is the
  61. ** first (only) file, and you are not in the background.
  62. **
  63. ** After calling this function to print a document, you need to call it
  64. ** again with a nil document handle.  The print record for the first (or only)
  65. ** document printed is preserved in a static variable.  This is so that the
  66. ** job dialog information can be passed on to documents 2-N in the print job.
  67. ** Calling this function with the document handle nil tells this function
  68. ** that you are done printing documents, and that the print record for the
  69. ** first job can be disposed of.
  70. */
  71.  
  72. #pragma segment Print
  73. OSErr    AppPrintDocument(FileRecHndl frHndl, Boolean jobDlg, Boolean firstJob)
  74. {
  75.     OSErr            err;
  76.     THPrint            prRecHndl;
  77.     TPPrPort        printPort;
  78.     GrafPtr            oldPort;
  79.     short            i, keepResFile, fstPage, lstPage, copies;
  80.     TPrStatus        status;
  81.     ControlHandle    proceedButton;
  82.     Rect             rct;
  83.  
  84.     static THPrint    prMergeHndl;
  85.  
  86.     if (!frHndl) {
  87.         if (prMergeHndl) {
  88.             DisposHandle((Handle)prMergeHndl);
  89.             prMergeHndl = nil;
  90.         }
  91.         return(noErr);
  92.     }
  93.  
  94.     PrintingStatusDialog = nil;
  95.  
  96.     if (!(prRecHndl = (THPrint)NewHandle(sizeof(TPrint)))) return(memFullErr);
  97.         /* If we can't generate a print record handle, we are out of here. */
  98.  
  99.     BlockMove((Ptr)&((*frHndl)->doc.print), (Ptr)(*prRecHndl), sizeof(TPrint));
  100.         /* Get the document's print info into the print record handle. */
  101.  
  102.     GetPort(&oldPort);
  103.  
  104.     DoSetCursor(&qd.arrow);
  105.     PrOpen();
  106.     err = PrError();
  107.  
  108.     if (!err) {
  109.         keepResFile = CurResFile();
  110.  
  111.         if (!(*frHndl)->doc.printRecValid) {
  112.             PrintDefault(prRecHndl);            /* The document print record was never 
  113.             err = PrError();                    ** initialized.  Now is is. */
  114.         }            
  115.         if (!err) {
  116.             PrValidate(prRecHndl);        /* Do this just 'cause Apple says so. */
  117.             err = PrError();
  118.         }
  119.         if (!err) {
  120.             if (jobDlg) {                /* User gets to click some buttons. */
  121.                 if (!(PrJobDialog(prRecHndl))) err = userCanceledErr;
  122.                 else                           err = PrError();
  123.             }
  124.         }
  125.         if (!err) {
  126.             if (!firstJob) {
  127.                 PrJobMerge(prMergeHndl, prRecHndl);
  128.                 err = PrError();
  129.             }
  130.         }
  131.  
  132.         if (!err) {            /* Put the defaulted/validated/jobDlg'ed print record in the doc. */
  133.             fstPage  = (*prRecHndl)->prJob.iFstPage;
  134.             lstPage  = (*prRecHndl)->prJob.iLstPage;
  135.             copies   = (*prRecHndl)->prJob.iCopies;
  136.             BlockMove((Ptr)(*prRecHndl), (Ptr)&((*frHndl)->doc.print), sizeof(TPrint));
  137.             (*frHndl)->doc.printRecValid = true;
  138.  
  139.             ParamText((*frHndl)->fileState.fss.name, nil, nil, nil);
  140.             PrintingStatusDialog = GetNewDialog(rPrStatusDlg, nil, (WindowPtr)-1);
  141.             if (PrintingStatusDialog) {
  142.                 GetDItem(PrintingStatusDialog, 1, &i, &(Handle)proceedButton, &rct);
  143.                 HiliteControl(proceedButton, 255);
  144.                     /* Setup the proceed/pause/cancel dialog with the document name. */
  145.                 (*prRecHndl)->prJob.pIdleProc = PrintIdleProc;
  146.                 UseResFile(keepResFile);
  147.                     /* Hook in the proceed/pause/cancel dialog. */
  148.             }
  149.  
  150.             for (i = 1; (i <= copies) && (!err); ++i) {
  151.  
  152.                 printPort = PrOpenDoc(prRecHndl, nil, nil);
  153.                 if (!(err = PrError())) {
  154.  
  155.                     gPrintPage = fstPage;
  156.                     while (gPrintPage <= lstPage) {
  157.  
  158.                         PrOpenPage(printPort, nil);
  159.  
  160.                         if (!(err = PrError()))
  161.                             ImageDocument(frHndl, false);
  162.                                 /* Do the print thing here. */
  163.  
  164.                         PrClosePage(printPort);
  165.  
  166.                         if (!gPrintPage) break;
  167.                         ++gPrintPage;
  168.                     }
  169.                     gPrintPage = 0;
  170.                     PrCloseDoc(printPort);
  171.                 }
  172.             }
  173.         }
  174.  
  175.         if (
  176.             (!err) &&
  177.             ((*prRecHndl)->prJob.bJDocLoop == bSpoolLoop) &&
  178.             (!(err = PrError()))
  179.         ) {
  180.             PrPicFile(prRecHndl, nil, nil, nil, &status);
  181.             err = PrError();
  182.         }
  183.     }
  184.  
  185.     if (firstJob) prMergeHndl = prRecHndl;
  186.     else          DisposHandle((Handle)prRecHndl);
  187.  
  188.     if (PrintingStatusDialog) DisposDialog(PrintingStatusDialog);
  189.  
  190.     PrClose();
  191.     SetPort(oldPort);
  192.  
  193.     return(err);
  194. }
  195.  
  196.  
  197.  
  198. /*****************************************************************************/
  199.  
  200.  
  201.  
  202. /* PrintIdleProc will handle events in the 'Printing Status Dialog' which
  203. ** gives the user the option to 'Proceed', 'Pause', or 'Cancel' the current
  204. ** printing job during print time.
  205. **
  206. ** The buttons:
  207. **        1: Proceed
  208. **        2: Pause
  209. **        3: Cancel 
  210. */
  211.  
  212. #pragma segment Print
  213. pascal void        PrintIdleProc(void)
  214. {
  215.     Boolean                button, paused;
  216.     ControlHandle        pauseButton, proceedButton;
  217.     DialogPtr            aDialog;
  218.     EventRecord            anEvent;
  219.     GrafPtr                oldPort;
  220.     Rect                 rct;
  221.     short                item, itemType, keepResFile;
  222.  
  223.     GetPort(&oldPort);
  224.  
  225.     UseResFile(keepResFile = CurResFile());
  226.  
  227.     GetDItem(PrintingStatusDialog, 1, &itemType, &(Handle)proceedButton, &rct);
  228.     HiliteControl(proceedButton, 255);
  229.     GetDItem(PrintingStatusDialog, 2, &itemType, &(Handle)pauseButton, &rct);
  230.  
  231.     paused = false;
  232.     do {
  233.         if (GetNextEvent((mDownMask + mUpMask + updateMask), &anEvent)) {
  234.             if (PrintingStatusDialog != FrontWindow ())
  235.             SelectWindow(PrintingStatusDialog);
  236.  
  237.             if (IsDialogEvent(&anEvent)) {
  238.                 button = DialogSelect(&anEvent, &aDialog, &item);
  239.  
  240.                 if ((button) && (aDialog == PrintingStatusDialog)) {
  241.                     switch (item) {
  242.                         case 1:
  243.                             HiliteControl(pauseButton, 0);        /* Enable PAUSE    */
  244.                             HiliteControl(proceedButton, 255);    /* Disable PROCEED */
  245.                             paused = false;
  246.                             break;
  247.                         case 2:
  248.                             HiliteControl(pauseButton, 255);    /* Disable PAUSE  */
  249.                             HiliteControl(proceedButton, 0);    /* Enable PROCEED */
  250.                             paused = true;
  251.                             break;
  252.                         case 3:
  253.                             PrSetError(iPrAbort);               /* CANCEL printing */
  254.                             paused = false;
  255.                             break;
  256.                     }
  257.                 }
  258.             }
  259.         }
  260.     } while (paused != false); 
  261.  
  262.     SetPort(oldPort);
  263. }
  264.  
  265.  
  266.  
  267. /*****************************************************************************/
  268.  
  269.  
  270.  
  271. #pragma segment Print
  272. OSErr    PresentStyleDialog(FileRecHndl frHndl)
  273. {
  274.     OSErr        err;
  275.     THPrint        prRecHndl;
  276.  
  277.     if (!(prRecHndl = (THPrint)NewHandle(sizeof(TPrint))))
  278.         return(memFullErr);
  279.  
  280.     PrOpen();
  281.  
  282.     if (!(err = PrError())) {
  283.  
  284.         BlockMove((Ptr)&(*frHndl)->doc.print, (Ptr)*prRecHndl, sizeof(TPrint));
  285.             /* Get data, valid or not. */
  286.  
  287.         if (!(*frHndl)->doc.printRecValid) PrintDefault(prRecHndl);
  288.         else                                PrValidate(prRecHndl);
  289.         if (!(err = PrError())) {
  290.             if (PrStlDialog(prRecHndl)) {
  291.                 BlockMove((Ptr)*prRecHndl, (Ptr)&(*frHndl)->doc.print, sizeof(TPrint));
  292.                 (*frHndl)->doc.printRecValid  = true;
  293.                 (*frHndl)->fileState.docDirty = true;
  294.             }
  295.             else err = userCanceledErr;
  296.         }
  297.     }
  298.  
  299.     DisposHandle((Handle)prRecHndl);
  300.     PrClose();
  301.  
  302.     return(err);
  303. }
  304.  
  305.  
  306.  
  307.